﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.BLL;
using MMS.Model;

namespace MyMMS
{
    public partial class FormShouyin : Form
    {
        public FormShouyin()
        {
            InitializeComponent();
        }

        private void FormShouyin_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FormShouyin_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                txtShuru.Focus();
            }
            else if (e.KeyCode == Keys.F5)
            {
                btnJiesuan_Click(null,null);
            }
        }

        private void btnJiesuan_Click(object sender, EventArgs e)
        {
            MessageBox.Show("当前没有数据");
        }
        private List<SettleGoods> buyList = new List<SettleGoods>();
        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataGridView da = dgvGoodInfo;
            GoodVoid GV = new GoodVoid();
            ///需要获取GoodsService类 里的返回值类型
            List<GoodsInfo> Good = GV.Good(txtShuru.Text);
            if (Good.Count == 0)
            {
                MessageBox.Show("查询的商品不存在");
            }
            else if(Good.Count==1)
            {
                GoodsInfo GS = Good[0];
                SettleGoods SG = new SettleGoods();
                SG.GoodsName = GS.GoodsName;
                SG.SalePrice = GS.SalePrice;
                SG.BuyCount = 1;
                buyList.Add(SG);
                Add();
            }
            else 
            {
                FormSPXuanze FSP = new FormSPXuanze();
                FSP.ShowData = Good;
                FSP.ShowDialog();
                GoodsInfo GS = FSP.SelectGoods;
                if (GS!=null)
                {
                    SettleGoods SG = new SettleGoods();
                    SG.GoodsName = GS.GoodsName;
                    SG.SalePrice = GS.SalePrice;
                    SG.BuyCount = 1;
                    buyList.Add(SG);
                    Add();
                    txtShuru.Text = "";
                }
                
            }
            
            //da.SelectedRows[0].SetValues("");
            ///用FOREACH 去向DGV 添加值？？？
            ///da.SelectedRows[0];
        }
        private void Add() 
        {
            dgvGoodInfo.AutoGenerateColumns = false;
            dgvGoodInfo.DataSource = new BindingList<SettleGoods>(buyList);
            double totalMoney = 0;
            int buyCount = 0;
            foreach (SettleGoods item in buyList)
            {
                totalMoney += item.CurrentPrice;
                buyCount += item.BuyCount;
            }
            lblCount.Text = buyCount.ToString();
            lblPrice.Text = totalMoney.ToString();
        }


        private void txtShuru_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnQuery_Click(null, null);
            }
        }

    }
}
